﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text;
using System.Xml.XPath;
using System.Net;

namespace Novice
{
    /// <summary>
    /// Summary description for transformation
    /// </summary>
    [WebService(Namespace = "http://soaiui.nmmu.ac.za/")]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Transformation : System.Web.Services.WebService
    {        
        [WebMethod]
        public string TransformXml(string xml, string xslt)
        {
            // Simple data checks  
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentException("XML Path cannot be null or empty", "xml");
            }
            if (string.IsNullOrEmpty(xslt))
            {
                throw new ArgumentException("XSLT Path cannot be null or empty", "xslt");
            }
            // Transformation settings
            XsltSettings xslt_set = new XsltSettings();
            xslt_set.EnableScript = true;
            xslt_set.EnableDocumentFunction = true;

            // Create required readers for working with xml and xslt  
            StringReader xsltInput = new StringReader(xslt);
            StringReader xmlInput = new StringReader(xml);
            XmlTextReader xsltReader = new XmlTextReader(xsltInput);
            XmlTextReader xmlReader = new XmlTextReader(xmlInput);

            // Create required writer for output  
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter transformedXml = new XmlTextWriter(stringWriter);

            // Create a XslCompiledTransform to perform transformation  
            XslCompiledTransform xsltTransform = new XslCompiledTransform();

            try
            {
                xsltTransform.Load(xsltReader, xslt_set, new XmlUrlResolver());
                xsltTransform.Transform(xmlReader, transformedXml);
            }
            catch (XmlException xmlEx)
            {
                // TODO : log - "Could not load XSL transform: \n\n" + xmlEx.Message  
                return "Could not load XSL transform" + xmlEx.Message;
            }
            catch (XsltException xsltEx)
            {
                // TODO : log - "Could not process the XSL: \n\n" + xsltEx.Message + "\nOn line " + xsltEx.LineNumber + " @ " + xsltEx.LinePosition)  
                return "Could not process XSL" + xsltEx.Message ;
            }
            catch (Exception ex)
            {
                // TODO : log  
                return "Unknow error occured during transformation: " + ex.Message;
            }

            return stringWriter.ToString();
        }  
    }
}
